#include "GameScreen.hpp"
#include "ScreenManager.hpp"

namespace fge
{

GameScreen::GameScreen()
	: 	hasFocus(false), isPopup(false), allowHiding(false), remove(false), managed(false), transitionOnTime(0),
		transitionOffTime(0), transitionPosition(0), screenState(ScreenState_Waiting)
{
}

GameScreen::~GameScreen()
{
}

void GameScreen::Update(float deltaSeconds, bool hasFocus, bool isCovered)
{
	this->hasFocus = hasFocus;

	if (isCovered)
		screenState = ScreenState_TransitionOff;

	switch (screenState)
	{
	case ScreenState_TransitionOn:
		if (transitionOnTime == 0)
			transitionPosition = 1;
		else
			transitionPosition += deltaSeconds / transitionOnTime;

		if (transitionPosition >= 1)
		{
			transitionPosition = 1;
			screenState = ScreenState_Active;
		}
		break;

	case ScreenState_TransitionOff:
		if (transitionOffTime == 0)
			transitionPosition = 0;
		else
			transitionPosition -= deltaSeconds / transitionOffTime;

		if (transitionPosition <= 0)
		{
			transitionPosition = 0;
			screenState = ScreenState_Hidden;
			if (!allowHiding)
			{
				remove = true;
				//screenManager->RemoveScreen(this);
			}
		}
		break;

	case ScreenState_Hidden:
	case ScreenState_Waiting:
		if (!isCovered)
			screenState = ScreenState_TransitionOn;
		break;
	}

	Update(deltaSeconds);
}

void GameScreen::ExitScreen(bool allowTransition)
{
	if (!allowTransition || transitionOffTime == 0)
	{
		remove = true;
		//screenManager->RemoveScreen(this);
	}
	else
	{
		allowHiding = false;
		screenState = ScreenState_TransitionOff;
	}
}

}
